﻿<ResourceDictionary
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:esriToolkit="clr-namespace:ESRI.ArcGIS.Client.Toolkit"
	xmlns:esriPrimitives="clr-namespace:ESRI.ArcGIS.Client.Toolkit.Primitives"
	xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
    >

	<!-- Default Legend Style-->
	<Style TargetType="esriToolkit:Legend">
		<Setter Property="Foreground" Value="Black" />
		<Setter Property="Background">
			<Setter.Value>
				<LinearGradientBrush EndPoint=".7,1" StartPoint=".7,0">
					<GradientStop Color="#FFFFFFFF" Offset="0"/>
					<GradientStop Color="#FFF9FAFB" Offset="0.375"/>
					<GradientStop Color="#FFE5E8EB" Offset="0.625"/>
					<GradientStop Color="#FFCBD1D7" Offset="1"/>
				</LinearGradientBrush>
			</Setter.Value>
		</Setter>

		<Setter Property="BorderBrush">
			<Setter.Value>
				<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
					<GradientStop Color="#FFA3AEB9" Offset="0"/>
					<GradientStop Color="#FF8399A9" Offset="0.375"/>
					<GradientStop Color="#FF718597" Offset="0.375"/>
					<GradientStop Color="#FF617584" Offset="1"/>
				</LinearGradientBrush>
			</Setter.Value>
		</Setter>
		<Setter Property="HorizontalContentAlignment" Value="Left" />
		<Setter Property="VerticalContentAlignment" Value="Top" />
		<Setter Property="Cursor" Value="Arrow" />
		<Setter Property="BorderThickness" Value="1" />	
		<Setter Property="Padding" Value="0" />
		<Setter Property="Margin" Value="0" />
		<Setter Property="IsTabStop" Value="False" />
		<Setter Property="TabNavigation" Value="Once" />
		<Setter Property="LayerItemsMode" Value="Flat" />		

		<!-- LegendItemTemplate = template for low level legend item : image + label-->
		<Setter Property="LegendItemTemplate">
			<Setter.Value>
				<DataTemplate>
					<StackPanel Orientation="Horizontal" Margin="0,-1">
						<Image Source="{Binding ImageSource}" HorizontalAlignment="Center" VerticalAlignment="Center"
							   Stretch="None" MinWidth="20" Margin="0,-1" />
						<TextBlock Text="{Binding Label}" Margin="5,0,0,0" VerticalAlignment="Center" />
					</StackPanel>
				</DataTemplate>
			</Setter.Value>
		</Setter>

		<!-- LayerTemplate = template for a layer in the legend -->
		<Setter Property="LayerTemplate">
			<Setter.Value>
				<DataTemplate>
					<StackPanel  Margin="0,-1" Orientation="Horizontal">
						<ToolTipService.ToolTip>
							<StackPanel MaxWidth="400">
								<TextBlock FontWeight="Bold" Text="{Binding ParentLabel}" TextWrapping="Wrap" />
								<TextBlock FontWeight="Bold" Text="{Binding Label}" TextWrapping="Wrap" />
								<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
								<TextBlock Text="{Binding SubLayerID, StringFormat='SubLayer ID : {0}'}" />
								<TextBlock Text="{Binding MinimumResolution, StringFormat='Minimum Resolution : {0:F6}'}" />
								<TextBlock Text="{Binding MaximumResolution, StringFormat='Maximum Resolution : {0:F6}'}" />
							</StackPanel>
						</ToolTipService.ToolTip>

						<TextBlock Text="{Binding Label}" VerticalAlignment="Center"/>
					</StackPanel>

				</DataTemplate>
			</Setter.Value>
		</Setter>

		<!-- Optional MapLayerTemplate (default is LayerTemplate) = template for a map layer -->
		<!-- A busy indicator and a slider for layer opacity make sense here-->
		<Setter Property="MapLayerTemplate">
			<Setter.Value>
				<DataTemplate>
					<StackPanel Margin="0,-1">
						<StackPanel.Resources>
							<!-- Busy indicator displayed while the legend is requested-->
							<DataTemplate x:Key="BusyIndicatorTemplate">
								<Grid x:Name="BusyIndicator" Background="Transparent" Margin="3,0" 
									  HorizontalAlignment="Left" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5" >
									<Grid.Triggers>
										<EventTrigger RoutedEvent="Grid.Loaded">
											<EventTrigger.Actions>
												<BeginStoryboard>
													<Storyboard>
														<DoubleAnimation Duration="0:0:1" RepeatBehavior="Forever" To="360" Storyboard.TargetName="BusyIndicator"
															 Storyboard.TargetProperty="(UIElement.RenderTransform).(RotateTransform.Angle)" />
													</Storyboard>
												</BeginStoryboard>
											</EventTrigger.Actions>
										</EventTrigger>
									</Grid.Triggers>

									<Grid.RenderTransform>
										<RotateTransform />
									</Grid.RenderTransform>
									<Ellipse Fill="#1E525252" Margin="11,2,11,20" Width="2" Height="2"/>
									<Ellipse Fill="#3F525252" Height="3" Width="3" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="0,4,5,0" />
									<Ellipse Fill="#7F525252" Width="4" HorizontalAlignment="Right" Height="4" VerticalAlignment="Top" Margin="0,9,1,0" />
									<Ellipse Fill="#BF525252" Height="5" Width="5" VerticalAlignment="Bottom" Margin="0,0,3,3" HorizontalAlignment="Right" />
									<Ellipse Fill="#FF525252" Height="6" Width="6" VerticalAlignment="Bottom" Margin="9,0" />
								</Grid>
							</DataTemplate>
						</StackPanel.Resources>


						<ToolTipService.ToolTip>
							<StackPanel MaxWidth="400">
								<TextBlock FontWeight="Bold" Text="{Binding Layer.CopyrightText}" TextWrapping="Wrap" />
								<TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
								<TextBlock Text="{Binding MinimumResolution, StringFormat='Minimum Resolution : {0:F6}'}" />
								<TextBlock Text="{Binding MaximumResolution, StringFormat='Maximum Resolution : {0:F6}'}" />
							</StackPanel>
						</ToolTipService.ToolTip>

						<StackPanel Orientation="Horizontal">

							<!--Busy indicator-->
							<ContentControl ContentTemplate="{StaticResource BusyIndicatorTemplate}"
											Visibility="{Binding BusyIndicatorVisibility}"/>

							<TextBlock Text="{Binding Label}" FontWeight="Bold" VerticalAlignment="Center" />
						</StackPanel>
					</StackPanel>

				</DataTemplate>
			</Setter.Value>
		</Setter>

		<!-- legend control template : here based on a tree view (extended to bind IsExpanded and IsSelected)-->		
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="esriToolkit:Legend">
					<esriPrimitives:TreeViewExtended	ItemsSource="{TemplateBinding LayerItemsSource}"                          
							Background="{TemplateBinding Background}"
							Foreground="{TemplateBinding Foreground}"
							BorderBrush="{TemplateBinding BorderBrush}"
							BorderThickness="{TemplateBinding BorderThickness}"      
							Padding="{TemplateBinding Padding}"
							HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
							VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
						>
						<esriPrimitives:TreeViewExtended.ItemTemplate>
							<sdk:HierarchicalDataTemplate ItemsSource="{Binding LayerItemsSource}" >
								<ContentPresenter Content="{Binding}" ContentTemplate="{Binding Template}" />
							</sdk:HierarchicalDataTemplate>
						</esriPrimitives:TreeViewExtended.ItemTemplate>
					</esriPrimitives:TreeViewExtended>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>

</ResourceDictionary>